package com.compomics.proteocloud.db.accessor;

import java.beans.PropertyChangeSupport;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Pepnovohit extends PepnovohitTableAccessor {

    /**
     * The spectrum name.
     */
    private String spectrumname;

    /**
     * Calls the super class.
     * @param params
     */
    public Pepnovohit(HashMap params){
        super(params);
    }
    
    /**
     * This constructor reads the spectrum file from a resultset. The ResultSet should be positioned such that a single
     * row can be read directly (i.e., without calling the 'next()' method on the ResultSet).
     *
     * @param aRS ResultSet to read the data from.
     * @throws SQLException when reading the ResultSet failed.
     */
    public Pepnovohit(ResultSet aRS) throws SQLException {
        super(aRS);
        this.spectrumname = aRS.getString("spectrumname");
    }
    
    public static List<Pepnovohit> getHitsFromSpectrumID(long aSpectrumID, Connection aConn) throws SQLException {
    	List<Pepnovohit> temp = new ArrayList<Pepnovohit>();
        PreparedStatement ps = aConn.prepareStatement(getBasicSelect() + " where l_spectrumid = ?");
        ps.setLong(1, aSpectrumID);
        ResultSet rs = ps.executeQuery();
        int counter = 0;
        while (rs.next()) {
            counter++;
            temp.add(new Pepnovohit(rs));
        }
        rs.close();
        ps.close();
        return temp;
    }

    /**
     * This method will find the hits from the current connection, based on the specified projectTitle.
     *
     * @param projectTitle The project title.
     * @param conn DB connection.
     * @return List of Pepnovo hits.
     * @throws SQLException when the retrieval did not succeed.
     */
    public static List<Pepnovohit> getHitsFromProjectTitle(String projectTitle, Connection conn) throws SQLException {
        List<Pepnovohit> temp = new ArrayList<Pepnovohit>();
        PreparedStatement ps = conn.prepareStatement("select ph.*, s.spectrumname from pepnovohit ph, spectrum s, project p where ph.l_spectrumid = s.spectrumid and s.l_projectid = p.projectid and p.title = ?");
        ps.setString(1, projectTitle);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            temp.add(new Pepnovohit(rs));
        }
        rs.close();
        ps.close();
        return temp;
    }

    /**
     * Returns the spectrum name.
     * @return The spectrum name.
     */
    public String getSpectrumName() {
        return spectrumname;
    }

    /**
     * This method returns the value for the 'Sequence' column.
     * @return	String with the value for the Sequence column.
     */
    public String getSequence() {
        if(iSequence.contains("+")) {
            int index = iSequence.indexOf("+");
            iSequence = iSequence.substring(0, index) +iSequence.substring(index + 3, iSequence.length());
        }
        return this.iSequence;
    }
}
